package com.example.demosecurityjwtlogin.controllers;

import com.example.demosecurityjwtlogin.models.Book;
import com.example.demosecurityjwtlogin.security.services.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/api/book")
public class BookController {
    @Autowired
    BookService bookService;

    @GetMapping("/")
    public List<Book> getAllBook() {
        return bookService.getAllBook();
    }

    @PostMapping("/add")
    @PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')")
    public int addBook(@RequestBody Book book) {
        return bookService.addBook(book);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('USER') or hasRole('MODERATOR') or hasRole('ADMIN')")
    public int deleteBookById(@PathVariable int id) {
        return bookService.deleteBookById(id);
    }

    @PutMapping("/up")
    @PreAuthorize("hasRole('MODERATOR')")
    public int updateBookById(@RequestBody Book book) {
        return bookService.updateBookById(book);
    }
}

//    @Autowired
//    BookService bookService;
//
//    @PostMapping("/")
//    public Book addBook(@RequestBody Book book) {  // 新增
//        bookService.addBook(book);
//        return book;
//    }
//
//    @PostMapping("/all")   // 批量新增
//    public List<Book> addAllBook(@RequestBody List<Book> books) {
//        bookService.addAllBook(books);
//        return books;
//    }
//
//    @DeleteMapping("/deleteAll")
//    public void deleteAll(@RequestBody List<Long> ids) {
//        bookService.deleteAll(ids);
//    }
////    @DeleteMapping("/{id}")
////    public String deleteById(@PathVariable Long id){
////        return bookService.deleteById(id);
////    }
//    @PutMapping("/{id}")
//    public String editBook(@RequestBody Book book){
//        return bookService.editBook(book);
//    }
////    @GetMapping("{id}")
////    public Book findById(@PathVariable Long id){
////        return bookService.findById(id);
////    }
//    @GetMapping("/")
//    public List<Book>findAll(){
//        return bookService.findAll();
//    }
//    @DeleteMapping("/{id}")
//    public String deleteById(@PathVariable Long id){
//        return bookService.deleteById(id);
//    }
////    @GetMapping("/")
////    public List<Book> findAdd(){
////        return bookService.findAll();
////    }
////    @GetMapping("/{page}/{size}")
////    public Page<Book> findById(@PathVariable int page, @PathVariable int size){
////        return bookService.getBookByPage(page,size);
////    }
//    @GetMapping("/author/{author}")
//    public List<Book> getBooksByAuthorStartingWith(String author){
//        return bookService.getBooksByAuthorStartingWith(author);
//    }
//
//    @GetMapping("/price/{price}")
//    public List<Book> getBooksByPriceGreaterThan(Float price){
//        return bookService.getBooksByPriceGreaterThan(price);
//    }
//    @GetMapping("/maxed")
//    public Book getMaxIdBook(){
//        return bookService.getMaxIdBook();
//    }
//
//    @GetMapping("/authorid")
//    public List<Book>getBookByIdAndAuthor(String author,Long id){
//        return bookService.getBookByIdAndAuthor(author,id);
//    }
//
//    @GetMapping("/nameid")
//    public List<Book>getBooksByIdAndName(String name,Long id){
//        return bookService.getBooksByIdAndName(name,id);
//    }
//
//
//    @GetMapping("/page")
//    public List<Book> getBookByPage(int pageNo,int size){
//        return bookService.getBookByPage(pageNo,size);
//    }
//}
